import program from 'commander';
import { version } from './utils/constants';
import main from './index';

console.log(version);
// 多种功能命令
const actionMap = {
  install: { // 配置命令的名字
    alias: 'i', // 命令别的名称
    description: 'install template', // 命令对应的描述
    examples: [// 命令对应的模板
      'zf-cli i',
      'zf-cli install',
    ],
  },
  config: {
    alias: 'c',
    description: 'config .zfclirc',
    examples: [
      'zf-cli config set <k> <v>',
      'zf-cli config get <k>',
      'zf-cli config remove <k>',
    ],
  },
  '*': {
    description: 'not found',
    alias: '',
    examples: [],
  },
};

Object.keys(actionMap).forEach((action) => {
  program.command(action)
    .description(actionMap[action].description)
    .alias(actionMap[action].alias)
    .action(() => {
      main(action, ...process.argv.slice(3));
    });
});

function help() {
  // console.log('123')//把example显示出去
  console.log('\r\n  ' + 'how to use command');
  Object.keys(actionMap).forEach((action) => {
    actionMap[action].examples.forEach((example) => {
      console.log(`  - ${example}`);
    });
  });
}
program.on('-h', help);
program.on('--help', help);
program.version(version).parse(process.argv);
// console.log(program.version(VERSION, '-v --version'));
// .parse(process.argv);
